﻿using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using Api.BLL;
using Api.Model;
using Newtonsoft.Json;
using System;
using Api.Common;
using Api.Web.helper;
using Api.Web.filter;

namespace Api.Web.Areas.Admin.Controllers
{
    [CheckAdminProject]
    public class CategoryController : AdminController
    {
        public ActionResult List()
        {
            List<ZTreeItem> treeItems = new List<ZTreeItem>();
            var list = CategoryBLL.Current.GetListNoTrack(s => s.ProjectId == projectId && s.UserId == userId);

            if (list == null)
            {
                ViewBag.Error = "數據庫錯誤";
                return View();
            }

            treeItems.AddRange(list.Select(s => new ZTreeItem()
            {
                id = s.ReallyId,//这个是js排序后的键
                pId = s.ParentId,//对应的是ReallyId列中数据，表示父节点
                name = s.Name,//显示名称
                dbId = s.Id, //自增主键                
            }));

            if (treeItems.Count == 0)
            {
                treeItems.Add(new ZTreeItem() { name = "" });
            }

            string nodes = "";
            try
            {
                if (treeItems.Count > 0)
                    nodes = JsonConvert.SerializeObject(treeItems);
            }
            catch (Exception ex)
            {
                ViewBag.Error = "序列化錯誤";
                return View();
            }

            ViewBag.Nodes = nodes;
            return View();
        }

        [HttpPost]
        public void Edit(string jsonItems)
        {
            #region 校验参数

            ZTree zTree;
            try
            {
                var zTreeItems = JsonConvert.DeserializeObject<List<ZTreeItem>>(jsonItems);
                if (zTreeItems == null)
                {
                    AjaxMsgHelper.ResponseFail("數據錯誤");
                    return;
                }

                zTree = new ZTree(zTreeItems.Count) { Items = zTreeItems };
            }
            catch
            {
                AjaxMsgHelper.ResponseFail("數據錯誤");
                return;
            }

            #endregion

            var length = zTree.Items.Count;

            var categoryList = new List<Category>(length);

            //不用区分那些是新增，那些是修改，那些是删除
            //直接就是删除之前添加的然后新增从客户端返回的，这样最简单了
            foreach (var item in zTree.Items)
            {                
                if (string.IsNullOrEmpty(item.name) && item.dbId == 0) //排除默認的那一條
                {
                    continue;
                }

                categoryList.Add(new Category()
                {
                    ProjectId = projectId,
                    Name = item.name,
                    ReallyId = item.id,
                    ParentId = item.pId,
                    Sort = item.sort,
                    UserId = Th.UserId,
                });
            }
                        
            bool result = true;
            try
            {
                CategoryBLL.Current.DeleteAndAdd(userId, projectId, categoryList);
            }
            catch
            {
                result = false;
            }

            if (result)
            {
                AjaxMsgHelper.ResponseSuccess("操作成功");
            }
            else
            {
                AjaxMsgHelper.ResponseFail("操作失敗");
            }
        }
    }
}